﻿using RayDen.Library.Core.Primitives;
using RayDen.RayEngine.Core.Library;
using RayDen.RayEngine.Core.Types;
using SmallVCM;

namespace RayDen.RayEngine.Data
{
    public class SMCamera : BaseCamera
    {
        private SmallVCM.Camera camera;
        public new float Fov
        {
            get { return fieldOfViewY; }
            set { fieldOfViewY = value; }
        }

        private float fieldOfViewY = 45f;
        private float fieldOfViewX = 120f;

        public float clipHither = 1f, clipYon = 120f;
        Vector dir, x, y;
        private int Width;
        private int Height;

        public SMCamera(Point eye, Vector dirt, Vector up, int width, int height, float fov = 65f)
        {
                this.Height = height;
            this.Width = width;
            this.Position = eye;
            this.Target = dirt;
            this.Up = up;
            this.fieldOfViewY = fov;
            camera = new Camera();
            camera.Setup(eye.ToVec(), dirt, -up, new UV(width, height), fov);
        }

        public override void GetRay(double x, double y, out RayData ray)
        {
            ray = new RayData();
            camera.GenerateRay(new UV((float)x, (float)y), out ray.Org, out ray.Dir);
        }

        public override void LookAt(Point eye, Point dirt, Vector up)
        {
            camera.Setup(eye.ToVec(), dirt.ToVec(), up, new UV(Width, Height), fieldOfViewY);
        }

        public override Point UnProject(ref Point v)
        {
            var pt = camera.WorldToRaster(v.ToVec());
            return new Point(pt.U, pt.V, 0);
        }
    }
}